package com.lxy.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.common.domain.common.R;
import com.lxy.common.domain.dto.MaintenanceScheduleDTO;
import com.lxy.common.domain.vo.MaintenanceSchedulesVO;
import com.lxy.equipment.domain.po.MaintenanceSchedules;
import com.lxy.equipment.domain.po.MaintenanceUsers;
import com.lxy.equipment.mapper.MaintenanceSchedulesMapper;
import com.lxy.equipment.service.MaintenanceSchedulesService;
import com.lxy.equipment.service.MaintenanceUsersService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class MaintenanceSchedulesServiceImpl extends ServiceImpl<MaintenanceSchedulesMapper, MaintenanceSchedules> implements MaintenanceSchedulesService {

    private final MaintenanceUsersService maintenanceUsersService;

    @Override
    public R addMaintenanceSchedule(MaintenanceScheduleDTO maintenanceScheduleDTO) {
        //创建维修计划表
        MaintenanceSchedules maintenanceSchedules=new MaintenanceSchedules();
        BeanUtils.copyProperties(maintenanceScheduleDTO,maintenanceSchedules);

        //存储数据
        save(maintenanceSchedules);
        return R.ok("创建维护计划成功");
    }

    @Override
    public R deleteMaintenanceSchedule(Long maintenanceId) {
        //删除MaintenanceUser
        LambdaQueryWrapper<MaintenanceUsers> UserlambdaQueryWrapper=new LambdaQueryWrapper<>();
        UserlambdaQueryWrapper.eq(MaintenanceUsers::getMaintenanceId,maintenanceId);
        maintenanceUsersService.remove(UserlambdaQueryWrapper);
        //删除MaintenanceSchedule
        removeById(maintenanceId);
        return R.ok("删除维修计划成功");
    }

    //返回所有维护计划
    @Override
    public R<List<MaintenanceSchedulesVO>> getAllMaintenanceSchedule() {
        //获取数据
        List<MaintenanceSchedules> list = list();
        List<MaintenanceSchedulesVO> maintenanceSchedulesVOS=new ArrayList<>();
        //数据封装
        for (MaintenanceSchedules maintenanceSchedules : list) {
            MaintenanceSchedulesVO maintenanceSchedulesVO=new MaintenanceSchedulesVO();
            BeanUtils.copyProperties(maintenanceSchedules,maintenanceSchedulesVO);

            //获取用户id
            LambdaQueryWrapper<MaintenanceUsers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(MaintenanceUsers::getMaintenanceId,maintenanceSchedules.getId());
            List<MaintenanceUsers> maintenanceUsers = maintenanceUsersService.list(lambdaQueryWrapper);
            List<Long> ids=new ArrayList<>();
            if(!maintenanceUsers.isEmpty()){
                for (MaintenanceUsers maintenanceUser : maintenanceUsers) {
                    ids.add(maintenanceUser.getUserId());
                }
            }
            maintenanceSchedulesVO.setIds(ids);
            maintenanceSchedulesVOS.add(maintenanceSchedulesVO);
        }
        //返回数据
        return R.ok(maintenanceSchedulesVOS);
    }

    @Override
    public R updateMaintenanceSchedule(MaintenanceSchedulesVO maintenanceSchedulesVO) {
        //封存数据
        MaintenanceSchedules maintenanceSchedules=new MaintenanceSchedules();
        BeanUtils.copyProperties(maintenanceSchedulesVO,maintenanceSchedules);
        //更新数据
        updateById(maintenanceSchedules);
        return R.ok("修改维护计划成功");
    }

    @Override
    public R<List<MaintenanceSchedulesVO>> getMyMaintenanceSchedules(Long userId) {
        //在MaintenanceUsers中查找用户有的维修计划id
        LambdaQueryWrapper<MaintenanceUsers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MaintenanceUsers::getUserId,userId);
        List<MaintenanceUsers> maintenanceUsers = maintenanceUsersService.list(lambdaQueryWrapper);
        //获取维修计划
        List<Long> ids=new ArrayList<>();
        for (MaintenanceUsers maintenanceUser : maintenanceUsers) {
            ids.add(maintenanceUser.getMaintenanceId());
        }
        List<MaintenanceSchedules> maintenanceSchedules = listByIds(ids);
        //封装数据
        List<MaintenanceSchedulesVO> maintenanceSchedulesVOS=new ArrayList<>();
        for (MaintenanceSchedules maintenanceSchedule : maintenanceSchedules) {
            MaintenanceSchedulesVO maintenanceSchedulesVO=new MaintenanceSchedulesVO();
            BeanUtils.copyProperties(maintenanceSchedule,maintenanceSchedulesVO);
            maintenanceSchedulesVOS.add(maintenanceSchedulesVO);
        }
        return R.ok(maintenanceSchedulesVOS);
    }

}
